<?php

function date_euro_to_unix_format($input) {
    // Assuming 6/12/2012 => 6-12-2012
    $date_array = explode('/', $input);
    return (is_array($date_array) && count($date_array) == 3) ? $date_array[0] . '-' . $date_array[1] . '-' . $date_array[2] : '1-1-1970';
}


function array_filter_numeric($input) {
    $result = array();
    if (is_array($input)) {
        foreach ($input as $key => $value) {
            if (is_numeric($value)) {
                $result[$key] = $value;
            }
        }
    }
    return $result;
}

function find_serie_deletion_starter($input) {
    $minor = -1;
    if (is_array($input)) {
        foreach ($input as $key => $value) {
            if ($value == 'n') {
                $minor = ($minor == -1 || $minor > $input[$key - 1]) ? $input[$key - 1] : $minor;
            }
        }
    }
    return $minor;
}

function filter_array($input) {
    if (is_array($input)) {
        foreach ($input as $key => $link) {
            if ($input[$key] == '') {
                unset($input[$key]);
            }
        }
    }
    return $input;
}


function ajax_sanitize_input_for_db_insert($input) {
    if (is_string($input)) {
        return '"' . addslashes($input) . '"';
    }
    $resultArray = array();
    foreach ($input as $item) {
        $resultArray[] = '"' . addslashes($item) . '"';
    }
    return $resultArray;
}


function object_to_array($d) {
    if (is_object($d)) {
        // Gets the properties of the given object
        // with get_object_vars function
        $d = get_object_vars($d);
    }
    if (is_array($d)) {
        /*
         * Return array converted to object
         * Using __FUNCTION__ (Magic constant)
         * for recursive call
         */
        return array_map(__FUNCTION__, $d);
    } else {
        // Return array
        return $d;
    }
}



function post_curl($url, $fields) {
    $fields_string = '';
    $fields_string = http_build_query($fields);
    rtrim($fields_string, '&');
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}
